package com.enfi.api.modules.bz.service.impl;

import com.enfi.api.modules.bz.service.CockpitService;
import com.enfi.api.modules.db.entity.IndexConfigCopy;
import com.enfi.api.modules.db.entity.UserCockpitConfig;
import com.enfi.api.modules.db.service.IndexConfigCopyService;
import com.enfi.api.modules.db.service.UserCockpitConfigService;
import com.enfi.api.util.SecUser;
import com.enfi.api.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author wind
 * @date 2020/4/10
 */
@Service
public class CockpitServiceImpl implements CockpitService {
    @Autowired
    private UserCockpitConfigService userCockpitConfigService;
    @Autowired
    private IndexConfigCopyService indexConfigCopyService;
    @Override
    public Object getCockpitIndexList(Integer cockpitType, Integer platformType) {
        Assert.notNull(cockpitType,"缺少类型");
        Assert.notNull(platformType,"缺少平台类型");
        SecUser secUser= UserUtils.getSecUser();
        IndexConfigCopy indexConfigCopy=new IndexConfigCopy();
        indexConfigCopy.setCockpitType(cockpitType);
        indexConfigCopy.setPlatformType(platformType);
        List<IndexConfigCopy> indexConfigCopyList = indexConfigCopyService.findAll(Example.of(indexConfigCopy), Sort.by("sortNum"));
        UserCockpitConfig userCockpitConfig=new UserCockpitConfig();
        userCockpitConfig.setCockpitType(cockpitType);
        userCockpitConfig.setPlatformType(platformType);
        userCockpitConfig.setUserId(secUser.getId());
        List<UserCockpitConfig> userCockpitConfigList = userCockpitConfigService.findAll(Example.of(userCockpitConfig));
        Map<Long,UserCockpitConfig> m=userCockpitConfigList.stream().collect(Collectors.toMap(UserCockpitConfig::getIndexConfigId, Function.identity(), (key1, key2) -> key2));
        indexConfigCopyList.forEach(v->{
            if(m.containsKey(v.getId())){
                v.setUserCockpitConfig(m.get(v.getId()));
            }
        });
        return indexConfigCopyList;
    }

    @Override
    public Object updateUserConfig(List<UserCockpitConfig> userCockpitConfigList) {
        SecUser secUser= UserUtils.getSecUser();
        Integer cockpitType=null;
        Integer platformType=null;
        if(!CollectionUtils.isEmpty(userCockpitConfigList)){
            cockpitType=userCockpitConfigList.get(0).getCockpitType();
        }
        Assert.notNull(cockpitType,"缺少类型");
        if(!CollectionUtils.isEmpty(userCockpitConfigList)){
            platformType=userCockpitConfigList.get(0).getPlatformType();
        }
        Assert.notNull(platformType,"缺少平台类型");
        UserCockpitConfig userCockpitConfig=new UserCockpitConfig();
        userCockpitConfig.setCockpitType(cockpitType);
        userCockpitConfig.setPlatformType(platformType);
        userCockpitConfig.setUserId(secUser.getId());
        List<UserCockpitConfig> userCockpitConfigs=userCockpitConfigService.findAll(Example.of(userCockpitConfig));
        userCockpitConfigService.deleteAll(userCockpitConfigs);
        for(int i=0;i<userCockpitConfigList.size();i++){
            UserCockpitConfig ucc = new UserCockpitConfig();
            ucc.setIsSelect(userCockpitConfigList.get(i).getIsSelect());
            ucc.setUserId(secUser.getId());
            ucc.setCockpitType(cockpitType);
            ucc.setPlatformType(platformType);
            ucc.setIndexConfigId(userCockpitConfigList.get(i).getIndexConfigId());
            ucc.setUpdateTime(new Date());
            userCockpitConfigService.save(ucc);
        }
        return true;
    }
}
